package cc.lj.internal.coupons.application.service;

import cc.lj.internal.core.model.PageBean;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.domain.martcap.model.vo.CapBatchDO;
import cc.lj.internal.coupons.domain.martcap.service.ICapBatchDomainService;
import cc.lj.internal.coupons.domain.ticket.model.vo.TicketBatchDO;
import cc.lj.internal.coupons.domain.ticket.repository.ITicketBatchLifecycleContinueRepository;
import cc.lj.internal.coupons.domain.ticket.service.ITicketBatchDomainService;
import cc.lj.internal.coupons.infrastructure.po.TicketBatchLifecycleContinuePO;
import cc.lj.internal.coupons.infrastructure.po.TicketBatchPO;
import cc.lj.internal.coupons.infrastructure.repository.TicketBatchLifecycleContinueRespository;
import cc.lj.internal.coupons.interfaces.dto.CapBatchDTO;
import cc.lj.internal.coupons.interfaces.dto.TicketBatchDTO;
import cc.lj.internal.coupons.interfaces.dto.command.InsertCapBatchCommand;
import cc.lj.internal.coupons.interfaces.dto.command.UpdateCapBatchCommand;
import cc.lj.internal.coupons.interfaces.dto.convert.CapBatchDTOConvertDO;
import cc.lj.internal.coupons.interfaces.dto.convert.TicketBatchDTOConvertDO;
import cc.lj.internal.coupons.interfaces.dto.query.CapBatchQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 电子券批次连续有效期表 app service
 * @author liangxifeng
 * @date 2021-12-20
 */
@Service
@Slf4j
public class TicketBatchAppService {
    //电子券批次定领域service
    @Resource(name = "ticketBatchDomainServiceImpl")
    private ITicketBatchDomainService batchDomainService;

    //电子券批次连续有效期repository
    @Resource(name = "ticketBatchLifecycleContinueRespository")
    private TicketBatchLifecycleContinueRespository continueRespository;

    /**
     * 查询未开始的所有券批次数据
     * @return
     */
    public List<TicketBatchDTO> getNoStartList(){
        List<TicketBatchDO> listDO = batchDomainService.getNoStartList();
        List<TicketBatchDTO> listDTO = new ArrayList<>();
        for (TicketBatchDO ticketBatchDO : listDO) {
            listDTO.add(TicketBatchDTOConvertDO.INSTANCE.convert(ticketBatchDO));
        }
        return listDTO;
    }

    /**
     * 查询在有效期内的券批次列表
     * 1、先查连续有效期表，查处所有在有效期内的batch编号；
     * 2、根据in查询所有券批次列表
     * @return
     * @author zhaoyong
     */
    public List<TicketBatchDTO> getValidList() {
        //1、先查出所有符合时间限制的批次
        TicketBatchLifecycleContinuePO continuePO = new TicketBatchLifecycleContinuePO();
        continuePO.setLifeEndDate(LocalDate.now());
        List<TicketBatchLifecycleContinuePO> listValid = continueRespository.getListValid(continuePO);

        //2、根据所有的batch_id查询批次列表
        List<Integer> allId  = new ArrayList<>();
        for(TicketBatchLifecycleContinuePO item : listValid)
        {
            allId.add(item.getLifeBatchId());
        }
        List<TicketBatchDO> listByIds = batchDomainService.getListByIds(allId);

        //3、返回数据
        List<TicketBatchDTO> resList = new ArrayList<>();
        for(TicketBatchDO ticketBatchDO : listByIds)
        {
            resList.add(TicketBatchDTOConvertDO.INSTANCE.convert(ticketBatchDO));
        }

        return resList;
    }
}
